﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace Lilium.Web.Mvc
{
	public static class ViewDataDictionaryExtensions
	{
		public static T GetInstance<T>(this ViewDataDictionary viewDataDictionary, bool required)
		{
			if (viewDataDictionary == null) throw new ArgumentNullException("viewDataDictionary");

			var instance = viewDataDictionary[typeof(T).FullName];
			
			if (required && (instance == null))
				throw new InvalidOperationException(string.Format("Instance of type {0} not found", typeof(T)));

			return (T)instance;
		}

		public static T GetInstance<T>(this ViewDataDictionary viewDataDictionary, string name, bool required)
		{
            //TODO: учитывать флаг required
			if (string.IsNullOrEmpty(name))
				return (T)viewDataDictionary[typeof(T).FullName];
			else
			{
				object untupedResult;
				if (!viewDataDictionary.TryGetValue(typeof(T).FullName + "+" + name, out untupedResult))
					viewDataDictionary.TryGetValue(typeof(T).FullName, out untupedResult);
				return (T)untupedResult;
			}
		}

		public static object GetParentModel(this ViewDataDictionary viewDataDictionary)
		{
			return GetParentModel<object>(viewDataDictionary);
		}

		public static T GetParentModel<T>(this ViewDataDictionary viewDataDictionary)
		{
			if (viewDataDictionary == null) throw new ArgumentNullException("viewDataDictionary");

			return (T)viewDataDictionary["__ParentModel"];
		}

		public static string GetValidationMessage(this ViewDataDictionary viewDataDictionary)
		{
			if (viewDataDictionary == null) throw new ArgumentNullException("viewDataDictionary");

			return viewDataDictionary["__ValidationMessage"] as string;
		}

		public static void SetInstance<T>(this ViewDataDictionary viewDataDictionary, T instance)
		{
			SetInstance(viewDataDictionary, instance, null, null);
		}

		public static void SetInstance<T>(this ViewDataDictionary viewDataDictionary, T instance, string name)
		{
            SetInstance(viewDataDictionary, instance, name, null);
		}

        public static void SetInstance<T>(this ViewDataDictionary viewDataDictionary, T instance, string name, Type type)
        {
            if (viewDataDictionary == null) throw new ArgumentNullException("viewDataDictionary");
            if (type == null)
                type = typeof(T);

            if (string.IsNullOrEmpty(name))
                viewDataDictionary[type.FullName] = instance;
            else
                viewDataDictionary[type.FullName + "+" + name] = instance;
        }

		public static void SetParentModel<T>(this ViewDataDictionary viewDataDictionary, T model)
		{
			if (viewDataDictionary == null) throw new ArgumentNullException("viewDataDictionary");

			viewDataDictionary["__ParentModel"] = model;
		}

		public static void SetValidationMessage(this ViewDataDictionary viewDataDictionary, string message)
		{
			if (viewDataDictionary == null) throw new ArgumentNullException("viewDataDictionary");

			viewDataDictionary["__ValidationMessage"] = message;
		}
	}
}
